import Input from './Input/Index'
import Select, { formatValue as selectFormatValue, formatInitValue as selectFormatInitValue } from './Select/Index'
import Textarea from './Textarea/Index'
import FileUpload, { formatValue as fileUploadFormatValue, formatInitValue as fileUploadFormatInitValue } from './FileUpload/Index'
import DatePicker, { formatValue as dateFormatValue, formatInitValue as dateFormatInitValue } from './DatePicker/Index'
import CallBackBtn from './CallBackBtn/Index'
import InputNumber from './InputNumber/Index'

export const componentMap = {
  文本框: Input,
  文本域: Textarea,
  下拉框: Select,
  文件: FileUpload,
  时间选择器: DatePicker,
  数字输入框: InputNumber,
  回调按钮: CallBackBtn
}

const formatSubmitMap = {
  文件: fileUploadFormatValue,
  时间选择器: dateFormatValue,
  下拉框: selectFormatValue
}

// 格式化提交数据
export async function formatComponentSubmitValue (d) {
  const f = formatSubmitMap[d.type]
  if (f) {
    return await f(d.value)
  }
  return d.value
}

const formatInitMap = {
  下拉框: selectFormatInitValue,
  时间选择器: dateFormatInitValue,
  文件: fileUploadFormatInitValue
}

export function formatComponentInitValue (d) {
  const f = formatInitMap[d.type]
  if (f) {
    return f(d.value, d.typeconfig || {})
  }
  return d.value
}
